import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


const routes = [
  //根目录，就是用户进来展示的就是首页
  {
    path: '/',
    name: 'index',
    component: () => import(/* webpackChunkName: "about" */ '../views/home.vue'),
  //在根目录下嵌套路由
  children: [
      {
        path: '/users',
        name: 'users',
        component: () => import(/* webpackChunkName: "about" */ '../views/users.vue'),
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "about" */ '../views/home.vue'),

  }
]

const router = new VueRouter({
  routes
})
//导航守卫，守卫home权限，判断有没有本地存储的
router.beforeEach((to, from, next) => {
  if (to.path == '/home' || to.path == '/') {
    if (!localStorage.getItem('userinfo')) {
      next('/login')
    }
  }
  // 如果用户未能验证身份，则 `next` 会被调用两次
  next()
})

export default router
